//
//  WKLoadWalletViewController.m
//  ourToken
//
//  Created by wakeup on 2018/6/6.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKLoadWalletViewController.h"
#import "WKBaseHeader.h"

@interface WKLoadWalletViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,WKLoadWalletCell1Delegate,WKLoadWalletCell2Delegate,WKLoadWalletCell3Delegate>

@property (weak, nonatomic) IBOutlet UIView *backView;//背景view

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;

@property (weak, nonatomic) IBOutlet UIButton *mnemonicBtn;//助记词按钮
@property (weak, nonatomic) IBOutlet UIView *mnemonicBottomView;

@property (weak, nonatomic) IBOutlet UIButton *walletBtn;//钱包按钮
@property (weak, nonatomic) IBOutlet UIView *walletBottomView;

@property (weak, nonatomic) IBOutlet UIButton *privateBtn;//私钥按钮
@property (weak, nonatomic) IBOutlet UIView *privateBottomView;

@property (nonatomic, strong) WKLoadWalletCell1 *cell1;
@property (nonatomic, strong) WKLoadWalletCell2 *cell2;
@property (nonatomic, strong) WKLoadWalletCell3 *cell3;

@property (nonatomic, copy) NSString *serialNumber;//钱包序列号

@end

@implementation WKLoadWalletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationView];
    [self layoutUI];
    self.backView.backgroundColor = AppManager.tableBackColor;
    [self getWalletSerialNumberNetworking];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.flowLayout.itemSize = CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height);
    
//    //默认显示第一张
//    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
//    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
}

- (void)setNavigationView {
    [super setNavigationView];
    self.navigationView.topTitleLabel.text = [NSString stringWithFormat:@"导入%@钱包",self.coinType];
}

- (void)layoutUI {
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.mnemonicBottomView.backgroundColor = AppManager.themeColor;
    self.walletBottomView.backgroundColor = AppManager.themeColor;
    self.privateBottomView.backgroundColor = AppManager.themeColor;
    [self setSelectedButtonFormTag:0];
    
    UINib *nib1 = [UINib nibWithNibName:@"WKLoadWalletCell1" bundle:nil];
    [self.collectionView registerNib:nib1 forCellWithReuseIdentifier:@"Cell1"];
    
    UINib *nib2 = [UINib nibWithNibName:@"WKLoadWalletCell2" bundle:nil];
    [self.collectionView registerNib:nib2 forCellWithReuseIdentifier:@"Cell2"];
    
    UINib *nib3 = [UINib nibWithNibName:@"WKLoadWalletCell3" bundle:nil];
    [self.collectionView registerNib:nib3 forCellWithReuseIdentifier:@"Cell3"];
}

//获取钱包序列号
- (void)getWalletSerialNumberNetworking {
    [httpManager getWalletSerialNumberSuccess:^(NSDictionary *result) {
        
        self.serialNumber = [result objectForKey:@"data"];
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - UICollectionViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.cell1 endEditing:YES];
    [self.cell2 endEditing:YES];
    [self.cell3 endEditing:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    float tempOffset = self.collectionView.contentOffset.x / self.collectionView.bounds.size.width;
    NSString *objA = [NSString stringWithFormat:@"%.f", tempOffset];
    int offset = [objA intValue];
    [self setSelectedButtonFormTag:offset];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.item == 0) {
        WKLoadWalletCell1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell1" forIndexPath:indexPath];
        self.cell1 = cell;
        self.cell1.delegate = self;
        return cell;
    }
    else if (indexPath.item == 1) {
        WKLoadWalletCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell2" forIndexPath:indexPath];
        self.cell2 = cell;
        self.cell2.delegate = self;
        return cell;
    }
    else {
        WKLoadWalletCell3 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell3" forIndexPath:indexPath];
        self.cell3 = cell;
        self.cell3.delegate = self;
        return cell;
    }
}

#pragma mark - 模块点击事件
//点击助记词按钮
- (IBAction)clickMnemonicBtn:(id)sender {
    [self setSelectedButtonFormTag:0];
    self.collectionView.contentOffset = CGPointMake(0, 0);
}

//点击钱包按钮
- (IBAction)clickWalletBtn:(id)sender {
    [self setSelectedButtonFormTag:1];
    self.collectionView.contentOffset = CGPointMake(self.collectionView.bounds.size.width, 0);
}

//点击私钥按钮
- (IBAction)clickPrivateBtn:(id)sender {
    [self setSelectedButtonFormTag:2];
    self.collectionView.contentOffset = CGPointMake(self.collectionView.bounds.size.width*2, 0);
}

#pragma mark - 设置模块颜色
- (void)setSelectedButtonFormTag:(int)tag {
    if (tag == 0) {
        [self.mnemonicBtn setTitleColor:AppManager.themeColor forState:UIControlStateNormal];
        self.mnemonicBottomView.hidden = NO;
        
        [self.walletBtn setTitleColor:AppManager.tableDetailColor forState:UIControlStateNormal];
        self.walletBottomView.hidden = YES;
        
        [self.privateBtn setTitleColor:AppManager.tableDetailColor forState:UIControlStateNormal];
        self.privateBottomView.hidden = YES;
    }
    else if (tag == 1) {
        [self.mnemonicBtn setTitleColor:AppManager.tableDetailColor forState:UIControlStateNormal];
        self.mnemonicBottomView.hidden = YES;
        
        [self.walletBtn setTitleColor:AppManager.themeColor forState:UIControlStateNormal];
        self.walletBottomView.hidden = NO;
        
        [self.privateBtn setTitleColor:AppManager.tableDetailColor forState:UIControlStateNormal];
        self.privateBottomView.hidden = YES;
    }
    else if (tag == 2) {
        [self.mnemonicBtn setTitleColor:AppManager.tableDetailColor forState:UIControlStateNormal];
        self.mnemonicBottomView.hidden = YES;
        
        [self.walletBtn setTitleColor:AppManager.tableDetailColor forState:UIControlStateNormal];
        self.walletBottomView.hidden = YES;
        
        [self.privateBtn setTitleColor:AppManager.themeColor forState:UIControlStateNormal];
        self.privateBottomView.hidden = NO;
    }
}

#pragma mark - WKLoadWalletCell1Delegate
- (void)textFieldDidBeginEditing {
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - 80, self.view.frame.size.width, self.view.frame.size.height);
}

- (void)textFieldDidEndEditing {
    self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 80, self.view.frame.size.width, self.view.frame.size.height);
}

#pragma mark - 开始导入
- (void)startImportWallet1 {
    
    if (self.isWelcome == YES) {
        WKRootViewController *root = [[WKRootViewController alloc] init];
        [self.navigationController pushViewController:root animated:YES];
    }
    else {
        
    }
    
    //苹果钱包地址 mmC8QHKUZwKTKpvWE5twnnrxsXi8mD86wU
    //苹果钱包私钥 cPb2wXhLCFYK1ViHWCoQbe4uJGwE8WHK8b59MhmsTw2mWojF1Hd7
    
    //苹果钱包A地址 mzSPRXg8YZCEcpFJkuwxTnnEkzcwbzMtQh
    //苹果钱包A私钥 cV2eGMGizaQwU9hfaZ3pS1PLG4xqcWPA7N3bUUuLAjurh2ZcN4ee
    
    //苹果钱包C地址 0xca431d17b79b41ac9acfb68f51d1aff9398f822b
    //苹果钱包C私钥 891660076867428925c739a7151e71d4aafc95bc9b4462abcdd181c5ddc5ebd9
    
    //苹果钱包D地址 0x3537eadac52c0741f07032cb655cb4535602533d
    //苹果钱包D私钥 ed3f43003f6020c4617790d41ee61201930acaeeeceecb7a99e3410b30b73ea3
    
//    NSArray *addressPrivateKeyArray;
//    if ([self.coinType isEqualToString:@"BTC"]) {
//        addressPrivateKeyArray = [WKBlockChainManager getPrivateAndAddressFormMnemonic:self.mnemonicLabel.text type:2];
//    }
//    else if ([self.coinType isEqualToString:@"ETH"]) {
//        addressPrivateKeyArray = [WKBlockChainManager getPrivateAndAddressFormMnemonic:self.mnemonicLabel.text type:3];
//    }
//
//    self.address = addressPrivateKeyArray[0];
//    self.private = addressPrivateKeyArray[1];
//    [[NSUserDefaults standardUserDefaults] setObject:self.address forKey:@"地址"];
//    [[NSUserDefaults standardUserDefaults] setObject:self.private forKey:@"私钥"];
//
//    NSString *haha = [[NSUserDefaults standardUserDefaults] objectForKey:@"地址"];
//    NSString *keke = [[NSUserDefaults standardUserDefaults] objectForKey:@"私钥"];
//    DLog(@"haha = %@",haha)
//    DLog(@"keke = %@",keke)
//
//    [httpManager foundWalletWithCoinType:self.coinType name:self.name.text address:self.address serialNumber:self.serialNumber createType:1 encryptedStr:self.mnemonicLabel.text success:^(NSDictionary *result) {
//
//    } failure:^(NSError *error) {
//
//    }];
}

- (void)startImportWallet2 {
    
}

- (void)startImportWallet3 {
    
}

//隐藏键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

@end
